package com.example.blogdemo.service;

import com.example.blogdemo.entity.Comment;
import com.example.blogdemo.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2017-12-18
 **/
@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private SensitiveService sensitiveService;

    public void addComment(Comment comment){
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveService.filter(comment.getContent()));
        commentRepository.save(comment);
    }

    public List<Comment> getCommentByArticleId(int articleId){
        return commentRepository.findCommentsByArticleId(articleId);
    }

    public  int getCommentsCount(int articleId){
        return commentRepository.findCommentCountByArticleId(articleId);
    }

    public void deleteComment(int commentId){
        commentRepository.updateStatus(commentId,1);
    }

    public Comment getCommentById(int commmentId){
        return commentRepository.findById(commmentId);
    }


}
